home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Code / Chapter10 / MultiViewpoints1Panel.java < prev    next >
Text File  |  2000-07-13  |  4KB  |  161 lines

  1. package applets;
  2.  
  3. import shout3d.*;
  4. import shout3d.core.*;
  5. import custom_nodes.*;
  6.  
  7.  
  8. public class MultiViewpoints1Panel extends Shout3DPanel implements DeviceObserver {
  9.    
  10.    
  11.    int pixelStartX;
  12.    int pixelStartY;
  13.    int pixelEndX;
  14.    int pixelEndY;
  15.  
  16.    TargetViewpoint[] cameras = new TargetViewpoint [3];
  17.    int currentCam = 1;
  18.    int totalCams = 1;
  19.  
  20.  
  21.    public MultiViewpoints1Panel(Shout3DApplet applet){
  22.       super(applet);
  23.    }
  24.       
  25.    public void customInitialize() {
  26.         
  27.       addDeviceObserver(this,"DeviceInput", null);
  28.       cameras[currentCam-1] = (TargetViewpoint) getCurrentBindableNode("Viewpoint");
  29.  
  30.    }
  31.  
  32.  
  33.    protected void finalize()  { 
  34.       removeDeviceObserver(this,"DeviceInput");
  35.    }
  36.  
  37.    
  38.    
  39.    public boolean onDeviceInput(DeviceInput di, Object userData) {
  40.      
  41.      //if mouse input
  42.      if (di instanceof MouseInput) {
  43.       MouseInput mi = (MouseInput) di;
  44.       switch (mi.which){
  45.  
  46.          case MouseInput.DOWN:
  47.             pixelStartX = mi.x;
  48.             pixelStartY = mi.y;
  49.             return true;
  50.  
  51.          case MouseInput.DRAG:
  52.  
  53.            //if left button used
  54.            if(mi.button == 0) {
  55.  
  56.             pixelEndX = mi.x;
  57.             pixelEndY = mi.y;
  58.  
  59.             int dragDistanceX = pixelEndX - pixelStartX;
  60.             int dragDistanceY = pixelEndY - pixelStartY;
  61.             
  62.             float headingDelta = dragDistanceX/70f;
  63.             float pitchDelta = dragDistanceY/70f;
  64.             
  65.             float temp = cameras[currentCam-1].heading.getValue() - headingDelta;
  66.             cameras[currentCam-1].heading.setValue(temp);
  67.  
  68.             temp = cameras[currentCam-1].pitch.getValue() - pitchDelta;
  69.             cameras[currentCam-1].pitch.setValue(temp);
  70.  
  71.          
  72.             pixelStartY = pixelEndY;
  73.             pixelStartX = pixelEndX;
  74.             return true;
  75.             
  76.            }//end of 0 if
  77.            
  78.  
  79.            //if right button used
  80.            if(mi.button == 1) {
  81.  
  82.             pixelEndY = mi.y;
  83.             int dragDistanceY = pixelEndY - pixelStartY;
  84.             
  85.             float distanceDelta = dragDistanceY/150f;
  86.             float temp = cameras[currentCam-1].distance.getValue() + distanceDelta;
  87.             cameras[currentCam-1].distance.setValue(temp);
  88.  
  89.             pixelStartY = pixelEndY;
  90.             return true;
  91.            }//end of 1 if
  92.          
  93.       }//end of switch
  94.  
  95.       return false;
  96.      }//end of mouse input
  97.  
  98.      if (di instanceof KeyboardInput) {
  99.  
  100.       KeyboardInput ki = (KeyboardInput) di;
  101.       if (ki.which == KeyboardInput.PRESS) {
  102.          if (ki.key == 'c' || ki.key == 'C') {
  103.  
  104.             //create new camera and
  105.             //add it to the array
  106.               if (totalCams < 3) {
  107.                totalCams++;
  108.                currentCam = totalCams;
  109.                cameras[currentCam-1] = new TargetViewpoint(this);
  110.  
  111.                //add to scene and bind
  112.                Node [] kids = {cameras[currentCam-1]};
  113.                getScene().addChildren(kids);
  114.                cameras[currentCam-1].isBound.setValue(true);
  115.                cameras[currentCam-1].center.setValue(cameras[0].center.getValue());
  116.    
  117.             }         
  118.          return true;
  119.          }//end of 'c'
  120.  
  121.  
  122.          if (ki.key == '1') {
  123.              if (totalCams >=  1) {
  124.                //bind camera 1
  125.                currentCam  =  1;
  126.                cameras[currentCam- 1].isBound.setValue(true);
  127.              }         
  128.            return true;
  129.          }//end of '1'
  130.  
  131.  
  132.          if (ki.key == '2') {
  133.              if (totalCams >=  2) {
  134.                currentCam  =  2;
  135.                cameras[currentCam- 1].isBound.setValue(true);
  136.              }         
  137.            return true;
  138.          }//end of '2'
  139.  
  140.  
  141.          if (ki.key == '3') {
  142.              if (totalCams >=  3) {
  143.                currentCam  =  3;
  144.                cameras[currentCam- 1].isBound.setValue(true);
  145.              }         
  146.            return true;
  147.          }//end of '3'
  148.  
  149.          return false;
  150.       }
  151.  
  152.       return false;
  153.  
  154.      }//end of keyboard input
  155.  
  156.      return false;
  157.  
  158.    }//end of onDeviceInput()
  159.  
  160.  
  161. }//end of class